<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <button id="postBtn">发起请求</button>
    <hr>
    <p id="content">

      </pre>
      <script>
        function handleSSE(message) {
            if (message.trim() === "") return; // 跳过空消息
            if (message.startsWith("data: ")) {
              return message.substring(5).split('\n')[0]; // 去掉 "data: ";
            }
            return message;
        }
        async function getData() {
          const res = await fetch("http://127.0.0.1:3000/post_step", {
            method: "post"
          });

          const reader = res.body.getReader();
          const textDecoder = new TextDecoder();

          while (1) {
            const {
              done,
              value
            } = await reader.read();
            if (done) break;
            const message = handleSSE(textDecoder.decode(value));
            content.innerText += message;
          }
        }
        postBtn.onclick = () => {
          content.innerText = "";
          getData();
        };
      </script>
  </body>
</html>